package net.wanho.ems.service.impl;

import cn.hutool.crypto.digest.MD5;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import net.wanho.ems.constant.UserConstant;
import net.wanho.ems.entity.User;
import net.wanho.ems.exception.LoginErrorException;
import net.wanho.ems.exception.PasswordMismatchException;
import net.wanho.ems.mapper.UserMapper;
import net.wanho.ems.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-07-06 16:31
 * Description：<描述>
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Override
    public void register(User user) {
        /**
         * 判断用户名是否存在
         */

        // 对密码进行加密
        user.setPassword(MD5.create().digestHex(user.getPassword()));
        // 新用户默认为禁用
        user.setStatus(UserConstant.USER_STATUS_DISABLE);
        // 创建时间为当前时间
        user.setRegistertime(new Date());
        userMapper.insert(user);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public User login(User user) {
        user.setPassword(MD5.create().digestHex(user.getPassword()));
        User u = userMapper.selectByusernameAndPassword(user);
        if (u == null) {
            throw new LoginErrorException("用户名或密码错误");
        }
        if(u.getStatus() == UserConstant.USER_STATUS_DISABLE){
            throw new LoginErrorException("用户已被禁用");
        }
        return u;
    }

    @Override
    public void modifyPassword(Integer id, String oldpassword, String newpassword) {
        // 判断旧密码是否正确
        User user = userMapper.selectByPrimaryKey(id);
        if (!user.getPassword().equals(MD5.create().digestHex(oldpassword))) {
            throw new PasswordMismatchException("旧密码错误");
        }
        // 修改密码
        user.setPassword(MD5.create().digestHex(newpassword));
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<User> list(Integer page, Integer limit, User user) {
        PageHelper.startPage(page,limit);
        List<User> list = userMapper.selectList(user);
        return new PageInfo<>(list);
    }

    @Override
    public void modifyStatus(Integer id, Integer status) {
        userMapper.updateStatusByPrimaryKey(id,status);
    }

    @Override
    public void removeById(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public User findById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void modify(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void resetPassword(Integer id) {
        userMapper.resetPassword(id,MD5.create().digestHex(UserConstant.DEFAULT_PASSWORD));
    }
}
